<template>
  <div>
    <!-- 列表页面 -->
    <div class="container" v-if="!showEdit">
      <div class="header"></div>
      <!-- 表格 -->
      <lin-table
        :tableColumn="tableColumn"
        :tableData="tableData"
        @row-click="rowClick"
        v-loading="loading"
      ></lin-table>
    </div>

    <!-- 编辑页面 -->
  </div>
</template>

<script>
import book from '@/api/book.js'
import LinTable from '@/component/base/table/lin-table'
import ParseTime from '@/lin/util/parse-time'

export default {
  components: {
    LinTable,
  },
  data() {
    return {
      tableColumn: [
        { prop: 'a', label: '产生时间' },
        { prop: 'b', label: '入库单号' },
        { prop: 'c', label: '客户' },
        { prop: 'd', label: '仓库代码' },
        { prop: 'e', label: '订单类型' },
        { prop: 'f', label: '计费金额' },
        { prop: 'g', label: '结算金额' },
        { prop: 'h', label: '订单状态' },
        { prop: 'i', label: '产品信息' },
      ],
      tableData: [
        {
          a: '2022-5-32 16:06:33',
          b: 'J001=2343',
          c: 'J233',
          d: 'SDFN【加拿大】',
          e: '入库单',
          f: '$35.21',
          g: '预付',
          h: '出单',
          i: 'SKU-EEEEE',
        },
      ],
      operate: [],
      showEdit: false,
      editBookID: 1,
    }
  },
  async created() {
    this.loading = true
    // await this.getBooks()
    this.loading = false
  },
  methods: {
    async getBooks() {
      try {
        const books = await book.getBooks()
        this.tableData = books
      } catch (error) {
        if (error.code === 10020) {
          this.tableData = []
        }
      }
    },
    handleEdit(val) {
      console.log('val', val)
      this.showEdit = true
      this.editBookID = val.row.id
    },
    handleDelete(val) {
      this.$confirm('此操作将永久删除该图书, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        const res = await book.deleteBook(val.row.id)
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.getBooks()
          this.$message({
            type: 'success',
            message: `${res.message}`,
          })
        }
      })
    },
    rowClick() {},
    editClose() {
      this.showEdit = false
      this.getBooks()
    },
    // 导出表格
    exprotExcel() {
      // 动态导入
      import('@/lin/util/export-excel').then(excel => {
        const tHeader = ['timestamp', 'title', 'label', 'importance', 'status']
        const filterVal = ['timestamp', 'title', 'label', 'importance', 'status']
        const data = this.formatJson(filterVal)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'table-list',
        })
      })
    },
    // 将表单格式化为json数据
    formatJson(filterVal) {
      return this.tableData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return ParseTime(v[j])
        }
        return v[j]
      }),)
    },
  },
}
</script>

<style lang="scss" scoped>
.container {
  padding: 0 30px;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title {
      height: 59px;
      line-height: 59px;
      color: $parent-title-color;
      font-size: 16px;
      font-weight: 500;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
    margin: 20px;
  }
}
</style>
